package com.rerun.controller.system;

import com.rerun.common.bean.QuartzReturnT;
import com.rerun.common.bean.Rest;
import com.rerun.common.controller.SuperController;
import com.rerun.common.quartz.QuartzJobBean;
import com.rerun.common.quartz.QuartzManager;
import com.rerun.entity.quartz.QrtzSchedulerState;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 任务监控
 *
 * @author dax.
 * @version v1.0
 * @since 2017 /10/24 16:42
 */
@Controller
@RequestMapping("/system/quartz")
public class QuartzController extends SuperController {
    @Resource
    private QuartzManager quartzManager;

    /**
     * Page string.
     *
     * @return the string
     */
    @RequestMapping("/list")
    @RequiresPermissions("quartzManager")
    public String page() {
        return "system/quartz/list";
    }

    /**
     * Index string.
     *
     * @return the string
     */
    @RequestMapping("/index")
    public String index() {
        return "system/quartz/table";
    }

    /**
     * Add job.
     *
     * @param jobBean the job bean
     * @return the rest
     */
    @RequestMapping("/add")
    @ResponseBody
    public Rest addJob(@RequestBody QuartzJobBean jobBean) {
        try {
            quartzManager.addJob(jobBean);
            return Rest.ok("添加成功");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return Rest.ok("添加任务失败");
    }

    /**
     * Pause job.
     *
     * @param jobBean the job bean
     * @return the rest
     */
    @RequestMapping("/pause")
    @ResponseBody
    public Rest jobPause(@RequestBody QuartzJobBean jobBean) {
        String jobName = jobBean.getJobName();
        String jobGroupName = jobBean.getJobGroupName();
        quartzManager.pauseJob(jobName, jobGroupName);
        return Rest.ok("暂停成功");
    }

    /**
     * Modify job cron.
     *
     * @param jobBean the job bean
     * @return the rest
     */
    @RequestMapping("/modifyCron")
    @ResponseBody
    public Rest modifyJobCron(@RequestBody QuartzJobBean jobBean) {
        quartzManager.modifyJobCron(jobBean);
        return Rest.ok("修改时间成功");
    }

    /**
     * Remove job.
     *
     * @param jobBean the job bean
     * @return the rest
     */
    @RequestMapping("/remove")
    @ResponseBody
    public Rest removeJob(@RequestBody QuartzJobBean jobBean) {
        quartzManager.removeJob(jobBean);
        return Rest.ok("移除成功");
    }

    /**
     * Resume job.
     *
     * @param jobBean the job bean
     * @return the rest
     */
    @RequestMapping("/resume")
    @ResponseBody
    public Rest resumeJob(@RequestBody QuartzJobBean jobBean) {
        String jobName = jobBean.getJobName();
        String jobGroupName = jobBean.getJobGroupName();
        quartzManager.resumeJob(jobName, jobGroupName);
        return Rest.ok("恢复成功");
    }

    /**
     * Gets all jobs.
     *
     * @return the all jobs
     * @throws SchedulerException the scheduler exception
     */
    @RequestMapping("/allJobs")
    @ResponseBody
    public QuartzReturnT getAllJobs() throws SchedulerException {

        return QuartzReturnT.okData(quartzManager.getAllJobs());
    }

    /**
     * Gets running jobs.
     *
     * @return the running jobs
     * @throws SchedulerException the scheduler exception
     */
    @RequestMapping("/runningJobs")
    @ResponseBody
    public QuartzReturnT getRunningJobs() throws SchedulerException {
        return QuartzReturnT.okData(quartzManager.getRunningJobs());
    }

    /**
     * Gets job.
     *
     * @param jobName      the job name
     * @param jobGroupName the job group name
     * @return the job
     * @throws SchedulerException the scheduler exception
     */
    @RequestMapping("/job")
    @ResponseBody
    public QuartzReturnT getJob(String jobName, String jobGroupName) throws SchedulerException {
        List<QuartzJobBean> quartzJobBeanList = new ArrayList<>();
        quartzJobBeanList.add(quartzManager.getJob(jobName, jobGroupName));
        return QuartzReturnT.okData(quartzJobBeanList);
    }

    /**
     * Gets all quartz instances.
     *
     * @return the all quartz instances
     */
    @RequestMapping("/currentSchedulerInstances")
    @ResponseBody
    public List<QrtzSchedulerState> getAllQuartzInstances() {
        return quartzManager.getAllQuartzInstances();
    }
}
